package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration //把当前的类变为xxx.xml 的配置类
public class CorsConfig {
    @Bean// 创建一个对象bean，对象交给spring容器管理
    public CorsWebFilter corsWebFilter(){
        //设置跨域的方式
        CorsConfiguration corsConfiguration=new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.setAllowCredentials(true);
// corsWebFilter 需要CorsConfigurationSource  CorsConfigurationSource 是一个接口，需要当前接口的实现类 UrlBasedCorsConfigurationSource是实现类
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        //第一个参数表示的为路径，第二个参数表示设置跨域的方式

        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(urlBasedCorsConfigurationSource);

    }
}
